using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using Mystery.SharePoint;

namespace Magical.Collaboration.Features.ProjectWebCustomization
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("cfad1c37-9ee6-4467-a65a-7f49b786fbe0")]
    public class ProjectWebCustomizationEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            web.ProvisionStandardLists();

            SPFile file = web.GetFile(web.RootFolder.WelcomePage);

            WikiPageModification wikiModification = new WikiPageModification(file);

            wikiModification.LoadTemplate(
                "<table id=\"layoutsTable1\" style=\"width:100%\">" +
                "	<tbody>" +
                "		<tr style=\"vertical-align:top\">" +
                "			<td style=\"width:50%\">" +
                "				[Tasks]" +
                "			</td>" +
                "			<td style=\"width:50%\">" +
                "				[Documents]" +
                "			</td>" +
                "		</tr>" +
                "	</tbody>" +
                "</table>");

            wikiModification.Register(web.Lists["Shared Documents"], "Documents");
            wikiModification.Register(web.Lists["Tasks"], "Tasks");
            wikiModification.Commit();

            bool updated = false;

            updated |= web.SetFriendlyTemplateName(Resources.Template_Project_ID);
            updated |= web.SetAvailableWebTemplates(new string[] { "MPS#0", "MPS#1", "MPS#2", "MPS#3", "MPS#4", "BLOG#0" });
            
            if (updated == true)
            {
                web.Properties.Update();
                web.Update();
            }
        }
    }
}
